package com.xinnet.core.validate;

import java.util.Properties;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.xinnet.core.validate.attribute.PostalCode;


/**
 * 邮政编码用的Validator类。
 * 
 * @author wenjianye
 * 请输入有效的邮政编码。
 */
public class PostalCodeValidator  extends Validator implements
ConstraintValidator<PostalCode,Object> {

  //private static final String REGEX_PATTERN_POSTAL_CODE = "([0-9]{7})?";
  private static final String REGEX_PATTERN_POSTAL_CODE = "([0-9]{6})?";
  private static Properties prop;
  @Override
	public void initialize(PostalCode arg0) {

	}

	/**
	 * 接口的验证方法
	 * */
	@Override
	public boolean isValid(Object value,
			ConstraintValidatorContext constraintContext) {

		return super.isValid(value, constraintContext);
	}

  public boolean valid(Object value) {
    return ValidatorUtil.patternMatches(REGEX_PATTERN_POSTAL_CODE, value);
  }
  /**
	 * 未通过验证时,提示错误信息
	 * */
	public String getMessage() {
		this.getClass().getCanonicalName();
		return prop
				.getProperty(this.getClass().getName());
	}
}
